<?php
/*--------------------------------
	返回值说明如下
		 0	发送成功
        -1	用户不存在
        -2	金额不足
        -3	号码格式不正确
        -4	号码个数超过100个
        -5	参数不完整
        -6	没有此项功能
        -7	网络故障

INSERT INTO `fanwe_sms` VALUES ('9', 'Soji', '易驰短信通', '', 'http://api.sojisms.com:8082/sendsms.aspx', 'seo', '123456', '', '0');
--------------------------------*/
include_once("Sms.class.php");

class SojiSms implements Sms
{

	public $message = "";
	public $smsInfo;
	
	public $statusStr = array( 
		"0" => "发送成功",
        "-1" => "用户不存在",
        "-2" => "金额不足",
        "-3" => "号码格式不正确",
        "-4" => "号码个数超过100个",
        "-5" => "参数不完整",
        "-6" => "没有此项功能",
        "-7" => "网络故障"
	);
	
    public function __construct($smsInfo = '')
    { 	
		if(!empty($smsInfo))
		{
			set_time_limit(0);
			
			$this->smsInfo = $smsInfo;
		}
    }
	
	public function sendSMS($mobiles=array(),$content,$sendTime='')
	{
		$mobileLen = 100;
		$mobileList = array_chunk($mobiles,$mobileLen);
		
		$content = a_utf8ToGB($content);
		$contentLen = mb_strlen($content,"GBK");
		$smsTotalCount = ceil($contentLen / 70) * count($mobiles);		
		$successNum = 0;
		$code = '';
		foreach($mobileList as $mobileItem)
		{
			$mobile = implode(",",$mobileItem);
			//$http = 'http://api.sojisms.com:8082/sendsms.aspx';
			$http = $this->smsInfo['server_url'];
			$data = array
			(
				'suser'=>$this->smsInfo['user_name'],					
				'spass'=>$this->smsInfo['password'],
				'telnum'=>$mobile,				
				'nr'=>$content,	
			);				
				
			$code= trim($this->postSMS($http,$data));
				
			$smsLog['send_content'] = a_gbToUtf8($content);;
			$smsLog['action_message'] = $this->statusStr[$code];
			$sendCount = count($mobileItem);
				
			if($code == "0")
			{
				$smsLog['success_mobiles'] = $mobile;
				$smsLog['fail_mobiles'] = "";
				$smsLog['success_count'] = $sendCount;
				$smsLog['fail_count'] = 0;
					
				$smsLog['expense_count'] = ceil($contentLen / 70) * $sendCount;

				$successNum += $sendCount;
			}
			else
			{
				$smsLog['success_mobiles'] = "";
				$smsLog['fail_mobiles'] = $mobile;
				$smsLog['success_count'] = 0;
				$smsLog['fail_count'] = $sendCount;
				$smsLog['expense_count'] = 0;
			}
				
			$smsLog['send_time'] = a_gmtTime();
				
			if(intval(a_fanweC('SMS_SEND_LOG')) == 1)
			{
				$sql = "insert into ".$GLOBALS['db_config']['DB_PREFIX']."sms_send_log (class_name,send_content,success_count,success_mobiles,fail_mobiles,expense_count,fail_count,action_message,send_time) values('Soji','".$smsLog['send_content']."','".$smsLog['success_count']."','".$smsLog['success_mobiles']."','".$smsLog['fail_mobiles']."','".$smsLog['expense_count']."','".$smsLog['fail_count']."','".$smsLog['action_message']."','".$smsLog['send_time']."')";
					
				$GLOBALS['db']->query($sql);
			}
		}
		
		if(($code == "0" && count($mobiles) == 1) || ($smsTotalCount == $successNum))
		{
			$this->message ="成功发送短信【".$content."】，到手机".implode(",",$mobiles);
			return 1;
		}
		else
		{
			$this->message = $smsTotalCount."条短信中，有".($smsTotalCount - $successNum)."条未成功发送到手机".implode(",",$mobiles);
			return 0;			
		}
	}
	
	function postSMS($url,$data='')
	{
		$row = parse_url($url);
		$host = $row['host'];
		$port = $row['port'] ? $row['port']:80;
		$file = $row['path'];
		while (list($k,$v) = each($data)) 
		{
			$post .= rawurlencode($k)."=".rawurlencode($v)."&";	//转URL标准码
		}
		$post = substr( $post , 0 , -1 );
		$len = strlen($post);
		$fp = @fsockopen( $host ,$port, $errno, $errstr, 10);
		if (!$fp) {
			return "$errstr ($errno)\n";
		} else {
			$receive = '';
			$out = "POST $file HTTP/1.1\r\n";
			$out .= "Host: $host\r\n";
			$out .= "Content-type: application/x-www-form-urlencoded\r\n";
			$out .= "Connection: Close\r\n";
			$out .= "Content-Length: $len\r\n\r\n";
			$out .= $post;		
			fwrite($fp, $out);
			while (!feof($fp)) {
				$receive .= fgets($fp, 128);
			}
			fclose($fp);
			$receive = explode("\r\n\r\n",$receive);
			unset($receive[0]);
			return implode("",$receive);
		}
	}	
}
?>